'''
Created on Sep 30, 2011

@author: hp
'''
import nltk
import constant
import os

from parser import Parser
wnl = nltk.WordNetLemmatizer()
parser = Parser()

def parse_sentence(sentence):
    
    sentence = sentence.lower()
    
    dependencies = parser.parseToStanfordDependencies(sentence)
    
    head = dict()
    children = dict()
    
    for rel, gov, dep in dependencies.dependencies:
        lem_gov = gov.text
        lem_dep = dep.text
        if constant.POS.has_key(dependencies.tokensToPosTags[gov]):
            lem_gov = wnl.lemmatize(gov.text, constant.POS[dependencies.tokensToPosTags[gov]])
        else:
            lem_gov = wnl.lemmatize(gov.text)
        if constant.POS.has_key(dependencies.tokensToPosTags[dep]):
            lem_dep = wnl.lemmatize(dep.text, constant.POS[dependencies.tokensToPosTags[dep]])
        else:
            lem_dep = wnl.lemmatize(dep.text)
        
        head[lem_dep] = (rel, lem_gov)
        if not children.has_key(lem_gov):
            children[lem_gov] = [(rel, lem_dep)]
        else:
            children[lem_gov].append((rel, lem_dep))
        
#    for word in head.keys():
#        print word, head[word]
#    for word in children.keys():
#        print word, children[word]
    
    return (head, children)

def _check_passive(verb, (head, children)):
    
    if not children.has_key(verb):
        return False
    for (rel,gov) in children[verb]:
        if rel.find('nsubjpass') != -1:
            return True

def parse_dep_tree(rverb, (head, children)):
    
    res = ""
    verb = rverb
    if not children.has_key(verb) and head.has_key(verb):
        verb = head[verb][1]

    # passive
    if _check_passive(verb, (head, children)):
        if children.has_key(verb):
            for (rel, gov) in children[verb]:
                if rel.find('agent') != -1:
                    res = res + "subj=%s\t" % (gov)
            if res.find('subj') == -1 and head.has_key(verb):
                for (rel, gov) in children[head[verb][1]]:
                    if rel.find('agent') != -1:
                        res = res + "subj=%s\t" % (gov)
                
        res = res + "verb=%s\t" % (rverb)
        if children.has_key(verb):        
            for (rel, gov) in children[verb]:
                if rel.find('nsubjpass') != -1:
                    res = res + "obj=%s\t" % (gov)
                if rel.find('dobj') != -1:
                    res = res + "dobj=%s\t" % (gov)
                if rel.find('prep_') != -1:
                    res = res + "prep%s=%s\t" %(rel[5:].upper(),gov)
                if rel.find('iobj') != -1:
                    res = res + "iobj=%s\t" % (gov)
                
    else:            
        if children.has_key(verb): 
            for (rel, gov) in children[verb]:
                if rel.find('subj') != -1:
                    res = res + "subj=%s\t" % (gov)
            if res.find('subj') == -1 and head.has_key(verb):
                for (rel, gov) in children[head[verb][1]]:
                    if rel.find('subj') != -1:
                        res = res + "subj=%s\t" % (gov)                
    
        res = res + "verb=%s\t" % (rverb)
        if children.has_key(verb):            
            for (rel, gov) in children[verb]:
                if rel.find('dobj') != -1:
                    res = res + "dobj=%s\t" % (gov)
                if rel.find('prep_') != -1:
                    res = res + "prep%s=%s\t" %(rel[5:].upper(),gov)
                if rel.find('iobj') != -1:
                    res = res + "iobj=%s\t" % (gov)    
                    
    print res
    return res

def parse_file(fname):
    
    fin = open(fname, "r")
    fout = open(fname + ".dp", 'w')
    verb = fname[4:fname.find('.')]
    try:
        for line in fin.read().split('\n'):
            if len(line) == 0: continue
            sent_id = line.split('\t')[0]
            sent = line.split('\t')[1]
            if len(sent) > 500:
                continue
            res = parse_dep_tree(verb, parse_sentence(sent))
            fout.write("%s\t%s\n" % (sent_id, res))
    finally:
        fin.close()
        fout.close()

def parse_folder():
    
    for fname in os.listdir("bnc"):
        if fname.find('.dp') != -1:
            continue
        if fname.find('explain') == -1:
            continue
        print "bnc/" + fname
        parse_file("bnc/" + fname)
        os.remove("bnc/" + fname)

if __name__ == "__main__":
    #parse_file("bnc/abandon.doc.1.complete")
    parse_folder()
    #parse_dep_tree('whisper', parse_sentence('You re trying to focus attention on the presenter , the person giving the speech or answering the questions and it is a dreadful distraction to have beside and behind him or her numbers of men and women who invariably want to whisper , gesticulate , stare in the wrong direction , scratch their heads , pick their noses or make their exits at completely the wrong moment . '))

